﻿Imports System.IO
Partial Public Class ClearImages
    Inherits System.Web.UI.Page
    Private photoPath As String = ConfigurationManager.AppSettings("photoPath")
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim imgAnnunci As List(Of AnnuncioImmagini) = DAL.Context.AnnuncioImmagini.ToList
            Dim diDocs As New DirectoryInfo(Server.MapPath(photoPath))
            Dim exceptions As New List(Of String)
            exceptions.Add("nopicture.jpg")
            exceptions.Add("Thumbs.db")
            For Each d As DirectoryInfo In diDocs.GetDirectories
                If d.Name <> ".svn" And d.Name <> "trash" Then
                    Dim dirName As String = d.Name
                    Dim imgToDelete As List(Of String) = (From docs As FileInfo In d.GetFiles Select docs.Name) _
                                    .Except(From i In imgAnnunci Where i.IDAnnuncio = New Guid(dirName) Select i.Percorso).Except(exceptions).ToList()
                    For Each docFile As String In imgToDelete
                        Dim newImgFile As String = dirName & "_" & docFile
                        File.Move(Server.MapPath(photoPath & dirName & "/" & docFile), Server.MapPath(photoPath & "trash/" & newImgFile))
                        ListBox1.Items.Add(New ListItem(docFile & " è stato spostato nella cartella '" & Server.MapPath(photoPath) & "trash'"))
                    Next
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub
End Class